package com.qf.blogsystem.dao.impl;

import com.qf.blogsystem.core.utils.DruidUtils;
import com.qf.blogsystem.dao.CommentDao;
import com.qf.blogsystem.entity.Comment;
import com.qf.blogsystem.vo.CommentVo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

/**
 * @author Crazy
 * @version 1.0
 * @description: 评论Dao实现类
 * @date 2022/12/6 11:06
 */
public class CommentDaoImpl implements CommentDao {
    /**
     * 创建DButils执行对象
     */
    private QueryRunner queryRunner=new QueryRunner(DruidUtils.getDataSource());

    /**
     * 添加评论信息
     * @param comment
     * @return Integer
     * @throws Exception
     */
    @Override
    public Integer insert(Comment comment) throws Exception {
        String sql = "insert into t_comment(userId,entityType,entityId,targetId,content,status,createTime) values(?,?,?,?,?,?,?)";
        return queryRunner.update(sql, comment.getUserId(), comment.getEntityType(), comment.getEntityId(), comment.getTargetId(), comment.getContent(), comment.getStatus(), comment.getCreateTime());
    }

    @Override
    public Comment selectById(Integer id) throws Exception {
        return null;
    }

    @Override
    public Integer updateById(Comment comment) throws Exception {
        return null;
    }

    @Override
    public Integer deleteById(Integer id) throws Exception {
        return null;
    }

    @Override
    public List<Comment> selectList() throws Exception {
        return null;
    }

    @Override
    public Integer batchDelete(List<Integer> ids) throws Exception {
        return null;
    }

    /**
     * 根据被评论的实体id查询评论信息
     * @param entityType
     * @param entityId
     * @return List<CommentVo>
     * @throws Exception
     */
    @Override
    public List<CommentVo> selectCommentByEntityId(Integer entityType ,Integer entityId) throws Exception {
        String sql = "select c.targetId , c.userId, c.content, c.createTime, u.username, u.headerUrl from t_comment c left join t_user u on (c.userId = u.id) where c.entityType = ? and c.entityId = ?";
        return queryRunner.query(sql,new BeanListHandler<CommentVo>(CommentVo.class),entityType,entityId);
    }
}
